#include "PointerItem.h"
#include <QDebug>
#include <QGraphicsScene>

#define Epslion 1e-8

CPointerItem::CPointerItem(CNeedleType type)
	: m_needleType(type)
	, m_angle(0.0)
{
	setTransformOriginPoint(0, 0);
}

QRectF CPointerItem::boundingRect() const
{
	return scene()->sceneRect();
}

void CPointerItem::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
{
	painter->setPen(Qt::NoPen);
	QRectF rect(-2, 40, 4, -200);

	switch (m_needleType)
	{
	case HourNeedle:
		painter->setBrush(QColor("#feffff"));
		rect.setRect(-9, 30, 18, -170);
		painter->drawRoundRect(rect, 5, 5);
		break;
	case MinuteNeedle:
		painter->setBrush(QColor("#00a5b3"));
		rect.setRect(-6, 60, 12, -220);
		painter->drawRoundRect(rect, 5, 5);
		break;
	case SecondNeedle:
		painter->setBrush(QColor("#ffea00"));
		painter->drawRoundRect(rect, 5, 5);
		painter->drawEllipse(-6, -6, 12, 12);
		break;
	default:
		break;
	}
}

void CPointerItem::setTimeValue(const QTime& value)
{
	int hour = value.hour() > 12 ? value.hour() - 12 : value.hour();
	switch (m_needleType)
	{
	case HourNeedle:
		m_angle = hour * 30 +
			value.minute() * 0.5 + value.second() * 0.00833333;
		break;
	case MinuteNeedle:
		m_angle = value.minute() * 6.0 + value.second() * 0.1;
		break;
	case SecondNeedle:
		m_angle = value.second() * 6.0;
		break;
	default:
		break;
	}
	
	setRotation(m_angle);
}


